package com.hzdp.web.action.account.ajax;

import org.springframework.beans.factory.annotation.Autowired;

import com.hzdp.cache.service.CacheService;
import com.hzdp.entity.UserAccount;
import com.hzdp.sms.bean.SmsSendResponse;
import com.hzdp.sms.service.SmsService;
import com.hzdp.user.service.UserAccountService;
import com.hzdp.web.action.AjaxBaseAction;
import com.hzdp.web.action.account.AccountConstants;
import com.hzdp.web.action.util.MobileRegexValidator;
import com.hzdp.web.captcha.JCaptchaService;
import com.hzdp.web.ops.remote.RemoteIpGetter;
import com.hzdp.web.util.CookieUtil;

public class AjaxSendVerifyMessageAction extends AjaxBaseAction {

	public static final int SUCCESS = 1;
	public static final int InvalidMobile = 3;
	public static final int MobileExists = 4;
	public static final int TooMuch = 2;
	public static final int InvalidCaptcha = 5;
	public static final int FAILED = 6;
	public static final String SmsCacheType = "sms";
	public static final String CaptchaCacheType = "captcha";

	@Autowired
	private SmsService smsService;
	@Autowired
	private CacheService cacheService;
	@Autowired
	private JCaptchaService jCaptchaService;
	@Autowired
	private UserAccountService userAccountService;

	private String phone;
	private String captcha;

	@Override
	protected boolean checkParams() {
		// 手机号码校验
		if (phone == null || phone.isEmpty() || !MobileRegexValidator.valid(phone)) {
			type = InvalidMobile;
			msg = "手机号码有误";
			return false;
		}

		UserAccount userAccount = userAccountService.findByMobile(phone);
		if(userAccount != null){
			type = MobileExists;
			msg = "当前手机号码已注册";
			return false;
		}
		return checkCaptcha();
	}

	// 图片验证码校验
	private boolean checkCaptcha() {
		if (captcha == null || captcha.isEmpty()) {
			type = InvalidCaptcha;
			msg = "图形验证码错误";
			return false;
		}

		if (!jCaptchaService.check(getUUID(), captcha)) {
			msg = "图形验证码错误";
			type = InvalidCaptcha;
			return false;
		}

		return true;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public void setCaptcha(String captcha) {
		this.captcha = captcha;
	}

	@Override
	protected void executeJson() {
		String remoteAddr = RemoteIpGetter.getRemoteAddr(request);
		SmsSendResponse sendResponse = smsService.sendVerifyCode(phone, remoteAddr);
		switch (sendResponse.getType()) {
		case SmsSendResponse.TOO_MUCH:
			type = TooMuch;
			msg = "发送太频繁啦~";
			break;
		case SmsSendResponse.WRONG_MOBILE:
			type = InvalidMobile;
			msg = "手机号码有误";
			break;
		case SmsSendResponse.FAIL:
			msg = "发送失败";
			type = FAILED;
			break;
		case SmsSendResponse.SUCCESS:
			msg = "短信动态码已发送，2分钟内输入有效";
			type = SUCCESS;
			break;
		default:
			break;
		}

		// 如果发送短信成功，注册时，就不需要校验图片验证码了。
		// 生成功token标识下，已经验证过图片。token有效期120秒
		CookieUtil.setCookie(response, AccountConstants.CaptchaCookieKey, AccountConstants.generateToken(getUUID(), captcha));
	}

	@Override
	public int getType() {
		return type;
	}

	@Override
	public int getStatus() {
		return status;
	}

	@Override
	public String getMsg() {
		return msg;
	}

}
